export default function getGodEvil(dateInfo, each, yinyang) {
  const result = {
    子: "",
    丑: "",
    寅: "",
    卯: "",
    辰: "",
    巳: "",
    午: "",
    未: "",
    申: "",
    酉: "",
    戌: "",
    亥: "",
  };
  //卦身
  if (each != undefined && yinyang != undefined) 卦身(result, each, yinyang);
  //日
  驿马(result, dateInfo);
  桃花(result, dateInfo);
  华盖(result, dateInfo);
  将星(result, dateInfo);
  劫煞(result, dateInfo);
  灾煞(result, dateInfo);
  禄神(result, dateInfo);
  文昌(result, dateInfo);
  羊刃(result, dateInfo);
  贵人(result, dateInfo);
  破碎(result, dateInfo);
  谋星(result, dateInfo);
  //月
  天医(result, dateInfo);
  // 地医(result, dateInfo);
  天马(result, dateInfo);
  天鸡(result, dateInfo);
  生气(result, dateInfo);
  死气(result, dateInfo);
  血支(result, dateInfo);
  孤辰(result, dateInfo);
  寡宿(result, dateInfo);
  天喜(result, dateInfo);
  //年
  病符(result, dateInfo);
  丧门(result, dateInfo);
  吊客(result, dateInfo);
  大耗(result, dateInfo);
  小耗(result, dateInfo);

  香闺(result, each, yinyang);
  床帐(result, each, yinyang);

  长生(result);
  沐浴(result);
  绝地(result);
  墓库(result);
  
  return result;
}

const branches = [
  "子",
  "丑",
  "寅",
  "卯",
  "辰",
  "巳",
  "午",
  "未",
  "申",
  "酉",
  "戌",
  "亥",
];

function getEarthBranch(any) {
  return any[1];
}
function getHeavenStem(any) {
  return any[0];
}
/**
 * 是否为白天: true白天, false夜晚
 * @param {四柱} dateInfo
 * @returns 是否为白天
 */
function isDay(dateInfo) {
  return ["卯", "辰", "巳", "午", "未", "申"].includes(
    getEarthBranch(dateInfo.time)
  );
}
/**
 * 移动指定offset，返回对应的地支
 * @param {从某个地支} fromBranch
 * @param {向左或向右移动几位} offset
 * @returns 返回对应的地支
 */
function findBranch(fromBranch, offset) {
  // 根据fromBranch找到其在数组中的索引
  var index = branches.indexOf(fromBranch);

  // 根据索引和offset计算新的索引
  var newIndex = (index + offset + branches.length) % branches.length;

  // 根据offset的正负情况处理newIndex
  if (offset < 0) {
    // 如果offset为负数，从数组末尾开始计算索引
    if (newIndex < 0) {
      newIndex = branches.length + newIndex;
    }
  } else if (offset >= branches.length) {
    // 如果offset大于等于数组长度，从数组开始继续旋转
    newIndex = newIndex % branches.length;
  }

  // 根据新的索引找到相应的地支并返回
  return branches[newIndex];
}

function findBranchIndex(branch) {
  return branches.indexOf(branch);
}

function 驿马(result, dateInfo) {
  const date = getEarthBranch(dateInfo.date);
  const name = "驿马 ";
  if (["寅", "午", "戌"].includes(date)) result.申 += name;
  if (["巳", "酉", "丑"].includes(date)) result.亥 += name;
  if (["亥", "卯", "未"].includes(date)) result.巳 += name;
  if (["申", "子", "辰"].includes(date)) result.寅 += name;
}

function 桃花(result, dateInfo) {
  const date = getEarthBranch(dateInfo.date);
  const name = "桃花 ";
  if (["寅", "午", "戌"].includes(date)) result.卯 += name;
  if (["巳", "酉", "丑"].includes(date)) result.午 += name;
  if (["亥", "卯", "未"].includes(date)) result.子 += name;
  if (["申", "子", "辰"].includes(date)) result.酉 += name;
}

function 华盖(result, dateInfo) {
  const date = getEarthBranch(dateInfo.date);
  const name = "华盖 ";
  if (["寅", "午", "戌"].includes(date)) result.戌 += name;
  if (["巳", "酉", "丑"].includes(date)) result.丑 += name;
  if (["亥", "卯", "未"].includes(date)) result.未 += name;
  if (["申", "子", "辰"].includes(date)) result.辰 += name;
}

function 将星(result, dateInfo) {
  const date = getEarthBranch(dateInfo.date);
  const name = "将星 ";
  if (["寅", "午", "戌"].includes(date)) result.午 += name;
  if (["巳", "酉", "丑"].includes(date)) result.酉 += name;
  if (["亥", "卯", "未"].includes(date)) result.卯 += name;
  if (["申", "子", "辰"].includes(date)) result.子 += name;
}

function 谋星(result, dateInfo) {
  const date = getEarthBranch(dateInfo.date);
  const name = "谋星 ";
  if (["寅", "午", "戌"].includes(date)) result.辰 += name;
  if (["巳", "酉", "丑"].includes(date)) result.未 += name;
  if (["亥", "卯", "未"].includes(date)) result.丑 += name;
  if (["申", "子", "辰"].includes(date)) result.戌 += name;
}

function 劫煞(result, dateInfo) {
  const date = getEarthBranch(dateInfo.date);
  const name = "劫煞 ";
  if (["寅", "午", "戌"].includes(date)) result.亥 += name;
  if (["巳", "酉", "丑"].includes(date)) result.寅 += name;
  if (["亥", "卯", "未"].includes(date)) result.申 += name;
  if (["申", "子", "辰"].includes(date)) result.巳 += name;
}
function 灾煞(result, dateInfo) {
  const date = getEarthBranch(dateInfo.date);
  const name = "灾煞 ";
  if (["寅", "午", "戌"].includes(date)) result.子 += name;
  if (["巳", "酉", "丑"].includes(date)) result.卯 += name;
  if (["亥", "卯", "未"].includes(date)) result.酉 += name;
  if (["申", "子", "辰"].includes(date)) result.午 += name;
}
function 破碎(result, dateInfo) {
  const date = getEarthBranch(dateInfo.date);
  const name = "破碎 ";
  if (["寅", "申", "巳", "亥"].includes(date)) result.酉 += name;
  if (["子", "午", "卯", "酉"].includes(date)) result.巳 += name;
  if (["辰", "戌", "丑", "未"].includes(date)) result.丑 += name;
}

function 禄神(result, dateInfo) {
  const date = getHeavenStem(dateInfo.date);
  const name = "禄神 ";
  if (["甲"].includes(date)) result.寅 += name;
  if (["乙"].includes(date)) result.卯 += name;
  if (["丙", "戊"].includes(date)) result.巳 += name;
  if (["丁", "己"].includes(date)) result.午 += name;
  if (["庚"].includes(date)) result.申 += name;
  if (["辛"].includes(date)) result.酉 += name;
  if (["壬"].includes(date)) result.亥 += name;
  if (["癸"].includes(date)) result.子 += name;
}

function 文昌(result, dateInfo) {
  const date = getHeavenStem(dateInfo.date);
  const name = "文昌 ";
  if (["甲"].includes(date)) result.巳 += name;
  if (["乙"].includes(date)) result.午 += name;
  if (["丙", "戊"].includes(date)) result.申 += name;
  if (["丁", "己"].includes(date)) result.酉 += name;
  if (["庚"].includes(date)) result.亥 += name;
  if (["辛"].includes(date)) result.子 += name;
  if (["壬"].includes(date)) result.寅 += name;
  if (["癸"].includes(date)) result.卯 += name;
}
function 羊刃(result, dateInfo) {
  const date = getHeavenStem(dateInfo.date);
  const name = "羊刃 ";
  if (["甲"].includes(date)) result.卯 += name;
  if (["乙"].includes(date)) result.寅 += name;
  if (["丙", "戊"].includes(date)) result.午 += name;
  if (["丁", "己"].includes(date)) result.巳 += name;
  if (["庚"].includes(date)) result.酉 += name;
  if (["辛"].includes(date)) result.申 += name;
  if (["壬"].includes(date)) result.子 += name;
  if (["癸"].includes(date)) result.亥 += name;
}

function 贵人(result, dateInfo) {
  const date = getHeavenStem(dateInfo.date);
  const name1 = "昼贵人 ";
  const name2 = "夜贵人 ";
  const _isDay = isDay(dateInfo);

  if (["甲", "戊"].includes(date)) {
    result.丑 += _isDay ? name1 : name2;
    result.未 += !_isDay ? name1 : name2;
  }
  if (["乙", "己"].includes(date)) {
    result.子 += _isDay ? name1 : name2;
    result.申 += !_isDay ? name1 : name2;
  }
  if (["丙", "丁"].includes(date)) {
    result.亥 += _isDay ? name1 : name2;
    result.酉 += !_isDay ? name1 : name2;
  }
  if (["壬", "癸"].includes(date)) {
    result.巳 += _isDay ? name1 : name2;
    result.卯 += !_isDay ? name1 : name2;
  }
  if (["庚", "辛"].includes(date)) {
    result.午 += _isDay ? name1 : name2;
    result.寅 += !_isDay ? name1 : name2;
  }
}

function 天医(result, dateInfo) {
  const month = getEarthBranch(dateInfo.month);
  const name = "天医 ";
  //朱辰彬老师说这里是-1，不是2，以后考虑一下是否需要调整
  // result[findBranch(month, 2)] += name;
  result[findBranch(month, -1)] += name;
}

function 地医(result, dateInfo) {
  const month = getEarthBranch(dateInfo.month);
  const name = "地医 ";
  // result[findBranch(month, 8)] += name;
  //因为调整了天医，地医也跟着调整
  result[findBranch(month, -7)] += name;
}

function 天马(result, dateInfo) {
  const month = getEarthBranch(dateInfo.month);
  const name = "天马 ";
  result[branches[findBranchIndex(month) * 2 + 2]] += name;
}

function 天鸡(result, dateInfo) {
  const month = getEarthBranch(dateInfo.month);
  const name = "天鸡 ";
  result[branches[10 - findBranchIndex(month) + 1]] += name;
}
function 生气(result, dateInfo) {
  const month = getEarthBranch(dateInfo.month);
  const name = "生气 ";
  result[findBranch(month, -2)] += name;
}
function 死气(result, dateInfo) {
  const month = getEarthBranch(dateInfo.month);
  const name = "死气 ";
  result[findBranch(month, -8)] += name;
}
function 血支(result, dateInfo) {
  const month = getEarthBranch(dateInfo.month);
  const name = "血支 ";
  result[findBranch(month, -1)] += name;
}
function 孤辰(result, dateInfo) {
  const month = getEarthBranch(dateInfo.month);
  const name = "孤辰 ";
  if (["亥", "子", "丑"].includes(month)) result.寅 += name;
  if (["寅", "卯", "辰"].includes(month)) result.巳 += name;
  if (["巳", "午", "未"].includes(month)) result.申 += name;
  if (["申", "酉", "戌"].includes(month)) result.亥 += name;
}
function 寡宿(result, dateInfo) {
  const month = getEarthBranch(dateInfo.month);
  const name = "寡宿 ";
  if (["亥", "子", "丑"].includes(month)) result.戌 += name;
  if (["寅", "卯", "辰"].includes(month)) result.丑 += name;
  if (["巳", "午", "未"].includes(month)) result.辰 += name;
  if (["申", "酉", "戌"].includes(month)) result.未 += name;
}

function 天喜(result, dateInfo) {
  const month = getEarthBranch(dateInfo.month);
  const name = "天喜 ";
  if (["亥", "子", "丑"].includes(month)) result.未 += name;
  if (["寅", "卯", "辰"].includes(month)) result.戌 += name;
  if (["巳", "午", "未"].includes(month)) result.丑 += name;
  if (["申", "酉", "戌"].includes(month)) result.辰 += name;
}

function 病符(result, dateInfo) {
  const year = getEarthBranch(dateInfo.year);
  const name = "病符 ";
  result[findBranch(year, -1)] += name;
}
function 丧门(result, dateInfo) {
  const year = getEarthBranch(dateInfo.year);
  const name = "丧门 ";
  result[findBranch(year, 2)] += name;
}
function 吊客(result, dateInfo) {
  const year = getEarthBranch(dateInfo.year);
  const name = "吊客 ";
  result[findBranch(year, -2)] += name;
}
function 大耗(result, dateInfo) {
  const year = getEarthBranch(dateInfo.year);
  const name = "大耗 ";
  result[findBranch(year, 6)] += name;
}
function 小耗(result, dateInfo) {
  const year = getEarthBranch(dateInfo.year);
  const name = "小耗 ";
  result[findBranch(year, 5)] += name;
}

function get卦身地支(each, yinyang) {
  if (yinyang == 7 || yinyang == 9) return branches[each];
  return branches[each + 6];
}

function 卦身(result, each, yinyang) {
  const name = "卦身 ";
  result[get卦身地支(each, yinyang)] += name;
}

function 香闺(result, each, yinyang) {
  const name = "香闺 ";
  const branch = get卦身地支(each, yinyang);
  if (["亥", "子"].includes(branch)) {
    result.巳 += name;
    result.午 += name;
  }
  else if (["寅", "卯"].includes(branch)) {
    result.辰 += name;
    result.戌 += name;
    result.丑 += name;
    result.未 += name;
  }
  else if (["巳", "午"].includes(branch)) {
    result.申 += name;
    result.酉 += name;
  }
  else if (["申", "酉"].includes(branch)) {
    result.寅 += name;
    result.卯 += name;
  }
  else if (["辰", "戌", "丑", "未"].includes(branch)) {
    result.亥 += name;
    result.子 += name;
  }
}

function 床帐(result, each, yinyang) {
  const name = "床帐 ";
  const branch = get卦身地支(each, yinyang);
  if (["亥", "子"].includes(branch)) {
    result.寅 += name;
    result.卯 += name;
  }
  else if (["寅", "卯"].includes(branch)) {
    result.巳 += name;
    result.午 += name;
  }
  else if (["巳", "午"].includes(branch)) {
    result.辰 += name;
    result.戌 += name;
    result.丑 += name;
    result.未 += name;
  }
  else if (["申", "酉"].includes(branch)) {
    result.亥 += name;
    result.子 += name;
  }
  else if (["辰", "戌", "丑", "未"].includes(branch)) {
    result.申 += name;
    result.酉 += name;
  }
}

function 长生(result) {
  result.申 += " 水土长 ";
  result.巳 += " 金长 ";
  result.寅 += " 火长 ";
  result.亥 += " 木长 ";
}

function 沐浴(result) {
  result.酉 += " 水土沐 ";
  result.午 += " 金沐 ";
  result.卯 += " 火沐 ";
  result.子 += " 木沐 ";
}

function 绝地(result) {
  result.巳 += " 水土绝 ";
  result.寅 += " 金绝 ";
  result.亥 += " 火绝 ";
  result.申 += " 木绝 ";
}
function 墓库(result) {
  result.辰 += " 水土库 ";
  result.丑 += " 金库 ";
  result.戌 += " 火库 ";
  result.未 += " 木库 ";
}
